//
//  YZChartView.m
//  shadow
//
//  Created by Pro on 2022/1/14.
//

#import "YZChartView.h"
#import "YZShadowView.h"



@interface YZChartView()
@property(nonatomic,assign)double yzMax;
@property(nonatomic,assign)double yzMin;
@property(nonatomic,assign)double yzAvarage;
@property(nonatomic,assign)double yzTop;
//@property(nonatomic,assign)double yzBottom;
@property(nonatomic,strong)YZShadowView * leftView;
@property(nonatomic,strong)YZShadowView * rightView;
@end

@implementation YZChartView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}

- (void)setDic:(NSDictionary *)dic {
    _dic = dic;
    CGFloat min = 10000;
    CGFloat max = 0;
    CGFloat total = 0.0;
    CGFloat average = 0.0;
    NSArray * dicInfo = [dic valueForKey:@"data"];
    NSArray * leftArr = [dicInfo valueForKey:@"left"];
    NSArray * rightArr = [dicInfo valueForKey:@"right"];
    NSMutableArray * arrayTotal = [NSMutableArray array];
    [arrayTotal addObjectsFromArray:leftArr];
    [arrayTotal addObjectsFromArray:rightArr];
    for (int i = 0 ; i < arrayTotal.count; i ++) {
        NSString * value = arrayTotal[i];
        CGFloat valueF = [value doubleValue];
        if (valueF > max) {
            max = valueF;
        }
        
        if (valueF < min) {
            min = valueF;
        }
        total += valueF;
    }
    self.yzMax = max;
    self.yzMin = min;
    NSInteger count = arrayTotal.count;
    average  = total / count;
    self.yzAvarage = average;
    self.yzTop = 200 + max;
    
//    2横坐标数据构造
    CGFloat width = 207;
    NSMutableArray * positionArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0 ; i < leftArr.count; i ++) {
        NSString * leftValue = leftArr[i];
        CGFloat leftValueF = [leftValue floatValue];
        CGFloat x;
        if (i == 0) {
            x = 0;
        }else {
            x = width * i / (leftArr.count - 1);

        }
        CGFloat y =  240 - leftValueF * 240 / self.yzTop;
        CGPoint point = CGPointMake(x, y);
        NSValue * value = [NSValue valueWithCGPoint:point];
        [positionArray addObject:value];
    }
    
    NSMutableArray * rightPositionnArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < rightArr.count; i ++) {
        NSString * rightValue = rightArr[i];
        CGFloat rightValueF = [rightValue floatValue];
        CGFloat x = width * i / leftArr.count;
        CGFloat y = 240 - rightValueF  * 240 / self.yzTop;
        CGPoint point = CGPointMake(x, y);
        NSValue * value = [NSValue valueWithCGPoint:point];
        [rightPositionnArray addObject:value];

    }
    
    self.leftView = [[YZShadowView alloc] initWithFrame:CGRectMake(0, 0, 207, 240) points:positionArray Color:[UIColor greenColor]];
    [self addSubview:self.leftView];
    
    self.rightView = [[YZShadowView alloc] initWithFrame:CGRectMake(207, 0, 207, 240) points:rightPositionnArray Color:[UIColor redColor]];
    [self addSubview:self.rightView];

}

@end
